import 'dart:convert';
import 'package:dio/dio.dart';
import 'package:flutter/services.dart';
import 'package:loxen/api/api.dart';
import 'package:loxen/api/http_server/user_info.dart';
import 'package:loxen/api/http_util.dart';
import 'package:loxen/mode/base_mode.dart';
import 'package:loxen/mode/mine/user_info_edit.dart';
import 'package:loxen/utils/string_res.dart';
import 'package:loxen/viewmode/base_view_model.dart';
import 'package:rxdart/rxdart.dart';

class UserInfoEditViewModel extends BaseViewModel with UserInfoMixin {
  UserInfoEdit userInfoEdit;
  List data;

  void loadData() {
    if (mState == BaseState.LOADING) return;
    userData().doOnListen(() {
      mState = BaseState.LOADING;
    }).flatMap((value) {
      if (value != null) {
        userInfoEdit = value;
        mState = BaseState.CONTENT;
      } else {
        mState = BaseState.EMPTY;
      }
      return rootBundle.loadString('assets/area.json').asStream();
    }).listen((event) {
      data = json.decode(event);
    }, onError: (error) {
      print(error);
      mState = BaseState.FAIL;
    }, onDone: () => notifyListeners());
  }

  void modifyUserInfo(UserInfoEdit userInfoEdit,
      {Function() success, Function(Exception error) onError}) {
    print(userInfoEdit.toJson());
    print(userInfoEdit.toString());
    ApiService.modifyUserInfo(
            userInfoEdit.company,
            userInfoEdit.position,
            userInfoEdit.email,
            userInfoEdit.sign,
            userInfoEdit.city,
            userInfoEdit.sexual)
        .listen((event) {
      if (event.statusCode == 200) {
        if (success != null) success.call();
      } else {
        if (onError != null) onError.call(Exception(event.data));
      }
    }, onError: (error) {
      if (onError != null) onError.call(error);
    });
  }

  void modifyNickname(String nickname,
      {Function() success, Function(String msg) onError}) {
    ApiService.modifyNickname(nickname).listen((event) {
      if (event.statusCode == 200) {
        if (success != null) success.call();
      } else {
        if (onError != null) onError.call('修改失败');
      }
    }, onError: (error) {
      print(error);
      if (error is ServiceException) {
        if (onError != null) onError.call(error.getErrorMsg('nickname'));
      }
    });
  }

  void modifyPortrait(FormData portrait,
      {Function() success, Function(Exception error) onError}) {
    ApiService.modifyPortrait(portrait).listen((event) {
      if (event.statusCode == 200) {
        this.userInfoEdit.portrait = event.data['data']['portrait'];
        notifyListeners();
        if (success != null) success.call();
      } else {
        if (onError != null) onError.call(Exception(event.data));
      }
    }, onError: (error) {
      if (onError != null) onError.call(error);
      if (error.portrait != null && error.portrait[0] != '') {
        print(error.portrait[0]);
      }
    });
  }

  static BaseMode<UserInfoEdit> decodeUserInfoEditBean(dynamic json) {
    return BaseMode<UserInfoEdit>(json);
  }
}
